package com.yunji.news.controller;

import com.yunji.news.common.Result;
import com.yunji.news.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 系统配置公共接口
 *
 * @author yunji
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/config")
@RequiredArgsConstructor
public class ConfigController {

    private final SystemConfigService systemConfigService;

    /**
     * 获取网站基础配置
     */
    @GetMapping("/website")
    public Result<Map<String, String>> getWebsiteConfig() {
        Map<String, String> config = systemConfigService.getConfigMapByGroup("website");
        return Result.success(config);
    }

    /**
     * 获取招生相关配置
     */
    @GetMapping("/admission")
    public Result<Map<String, String>> getAdmissionConfig() {
        Map<String, String> config = systemConfigService.getConfigMapByGroup("admission");
        return Result.success(config);
    }

    /**
     * 获取系统公告配置
     */
    @GetMapping("/announcement")
    public Result<Map<String, String>> getAnnouncementConfig() {
        Map<String, String> config = systemConfigService.getConfigMapByGroup("announcement");
        return Result.success(config);
    }

    /**
     * 获取联系方式配置
     */
    @GetMapping("/contact")
    public Result<Map<String, String>> getContactConfig() {
        Map<String, String> config = systemConfigService.getConfigMapByGroup("contact");
        return Result.success(config);
    }

    /**
     * 根据配置键获取单个配置值
     */
    @GetMapping("/value/{configKey}")
    public Result<String> getConfigValue(@PathVariable String configKey) {
        String value = systemConfigService.getConfigValue(configKey);
        return Result.successWithStringData(value);
    }

    /**
     * 获取应用信息
     */
    @GetMapping("/app-info")
    public Result<Map<String, Object>> getAppInfo() {
        Map<String, String> websiteConfig = systemConfigService.getConfigMapByGroup("website");
        
        // 构建应用信息
        Map<String, Object> appInfo = new HashMap<>();
        appInfo.put("name", websiteConfig.getOrDefault("site_name", "浙江高职提前招生网"));
        appInfo.put("version", "1.0.0");
        appInfo.put("description", websiteConfig.getOrDefault("site_description", "浙江省高职提前招生信息服务平台"));
        appInfo.put("keywords", websiteConfig.getOrDefault("site_keywords", "高职,提前招生,浙江"));
        appInfo.put("icp", websiteConfig.getOrDefault("icp_number", ""));
        appInfo.put("copyright", websiteConfig.getOrDefault("copyright", ""));
        
        return Result.success(appInfo);
    }

    /**
     * 获取上传配置
     */
    @GetMapping("/upload")
    public Result<Map<String, Object>> getUploadConfig() {
        Map<String, String> uploadConfig = systemConfigService.getConfigMapByGroup("upload");
        
        // 转换为前端需要的格式
        Map<String, Object> config = new HashMap<>();
        config.put("maxFileSize", Integer.parseInt(uploadConfig.getOrDefault("max_file_size", "10485760"))); // 10MB
        config.put("allowedImageTypes", uploadConfig.getOrDefault("allowed_image_types", "jpg,jpeg,png,gif,bmp,webp").split(","));
        config.put("allowedDocumentTypes", uploadConfig.getOrDefault("allowed_document_types", "pdf,doc,docx,xls,xlsx,ppt,pptx,txt").split(","));
        config.put("uploadPath", uploadConfig.getOrDefault("upload_path", "/uploads/"));
        
        return Result.success(config);
    }
}